<template>
  <el-dialog title="分配角色" :visible="showRoleDailog" @close="cancle">
    <el-row>
      <el-checkbox-group v-model="checkList" @change="check">
        <el-checkbox v-for="role in roleList" :key="role.id" :label="role.id">{{ role.name }}</el-checkbox>
      </el-checkbox-group>
    </el-row>
    <el-row type="flex" justify="center" style="margin-top:50px">
      <el-button @click="assign">确定</el-button>
      <el-button @click="cancle">取消</el-button>
    </el-row>
  </el-dialog>
</template>

<script>
import { getRole } from '@/api/setting'
import { assignRole } from '@/api/employees'
import { getUserDetailById } from '@/api/user'
export default {
  props: {
    showRoleDailog: {
      type: Boolean
    },
    id: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      checkList: [],
      roleList: [],
      roleIds: []
    }
  },
  created() {
    // this.getRole()
    // getUserDetailById(this.id).then(res => {
    // //   console.log(res)
    //   console.log(res.roleIds)
    //   this.checkList = res.roleIds
    // })
  },
  methods: {
    cancle() {
      this.$emit('update:showRoleDailog', false)
    },
    async getRole() {
      const { rows } = await getRole()
      this.roleList = rows
    },
    async getEmployeeDetail() {
      const res = await getUserDetailById(this.id)
      this.checkList = res.roleIds
    },
    check(val) {
      this.roleIds = val
    },
    async assign() {
      const data = { id: this.id, roleIds: this.roleIds }
      await assignRole(data)
      this.$emit('update:showRoleDailog', false)
      this.$message.success('修改成功')
    }
  }
}
</script>

<style>

</style>
